package com.yn.xgame.game;

import com.yn.xgame.GlobalSetting;
import com.yn.xgame.ZkClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * GameCenterManager
 * Created by wangboo on 17/7/16.
 */
public class GameCenterManager {

    private static final Logger logger = LoggerFactory.getLogger(GameCenterManager.class);
    private int id;
    // niuniu - CenterManager
    // benz   - CenterManager
    private Map<String, CenterManager> map = new HashMap<>();

    public GameCenterManager(int id) {
        this.id = id;
    }


    void addCenterManager(CenterManager mgr) {
        map.put(mgr.getName(), mgr);
    }

    public CenterManager getCenterManager(String name) {
        return map.get(name);
    }

    // 将管理的子游戏，和自己的信息注册到zk上
    public void reportToZK(ZkClient client) {
        String path = GlobalSetting.ZK_PREFIX + "/center/" + id;
        String names = StringUtils.join(map.keySet(), ",");
        client.createOrsetData_EPHEMERAL(path, names, false);
        logger.debug("regist GameCenter to path: {}, games: {}", path, names);
    }

    public int getUserCount() {
        int userCount = 0;
        for (CenterManager center : map.values()) {
            userCount += center.getUserCount();
        }
        return userCount;
    }

    public int getId() {
        return id;
    }

    public Set<String> getGameList() {
        return map.keySet();
    }
}
